<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>FAQ：命名空间必知必会</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="language.namespaces.rules.html">« 名称解析规则</a></li>
      <li style="float: right;"><a href="language.enumerations.html">枚举 »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="language.namespaces.html">命名空间</a></li>
    <li>FAQ：命名空间必知必会</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="language.namespaces.faq" class="sect1">
  <h2 class="title">FAQ：命名空间必知必会</h2>
  <p class="verinfo">(PHP 5 &gt;= 5.3.0, PHP 7, PHP 8)</p>
  <p class="para">
   本文分两节：常见问题、有助于完全理解的实现详情。
  </p>
  <p class="para">
   首先，常见问题。
   <ol type="1">
    <li class="listitem">
     <span class="simpara">
      <a href="language.namespaces.faq.html#language.namespaces.faq.shouldicare" class="link">如果我不用命名空间，是否需要关心它？</a>
     </span>
    </li>
    <li class="listitem">
     <span class="simpara">
      <a href="language.namespaces.faq.html#language.namespaces.faq.globalclass" class="link">我如何在命名空间内使用一个全局/内置的类？</a>
     </span>
    </li>
    <li class="listitem">
     <span class="simpara">
      <a href="language.namespaces.faq.html#language.namespaces.faq.innamespace" class="link">
         如何在命名空间内访问它自己的类、函数、常量？</a>
     </span>
    </li>
    <li class="listitem">
     <span class="simpara">
      <a href="language.namespaces.faq.html#language.namespaces.faq.full" class="link">
       像 <code class="literal">\my\name</code> 和 <code class="literal">\name</code> 这样的名称是如何解析的？
      </a>
     </span>
    </li>
    <li class="listitem">
     <span class="simpara">
      <a href="language.namespaces.faq.html#language.namespaces.faq.qualified" class="link">像
      <code class="literal">my\name</code> 这样的名称是如何解析的？</a>
     </span>
    </li>
    <li class="listitem">
     <span class="simpara">
      <a href="language.namespaces.faq.html#language.namespaces.faq.shortname1" class="link">像 <code class="literal">name</code> 这样的非限定类名是如何解析的？</a>
     </span>
    </li>
    <li class="listitem">
     <span class="simpara">
      <a href="language.namespaces.faq.html#language.namespaces.faq.shortname2" class="link">像 <code class="literal">name</code> 这样的非限定常量和函数名是如何解析的？</a>
     </span>
    </li>
   </ol>
  </p>
  <p class="para">
   为了帮助理解，我们提供了一些命名空间实现细节。
   <ol type="1">
    <li class="listitem">
     <span class="simpara">
      <a href="language.namespaces.faq.html#language.namespaces.faq.conflict" class="link">在同一个文件中，导入名称不能和定义的类名发生冲突。</a>
     </span>
    </li>
    <li class="listitem">
     <span class="simpara">
      <a href="language.namespaces.faq.html#language.namespaces.faq.nested" class="link">不允许嵌套 namespace。
      </a>
     </span>
    </li>
    <li class="listitem">
     <span class="simpara">
      <a href="language.namespaces.faq.html#language.namespaces.faq.quote" class="link">动态命名空间名称（引号标识）应该转义反斜线。</a>
     </span>
    </li>
    <li class="listitem">
     <span class="simpara">
      <a href="language.namespaces.faq.html#language.namespaces.faq.constants" class="link">引用一个未定义的、带反斜线的常量，会导致 fatal 错误并退出</a>
     </span>
    </li>
    <li class="listitem">
     <span class="simpara">
      <a href="language.namespaces.faq.html#language.namespaces.faq.builtinconst" class="link">不能重载特殊常量：
       NULL、TRUE、FALSE、ZEND_THREAD_SAFE、ZEND_DEBUG_BUILD</a>
     </span>
    </li>
   </ol>
  </p>
  <div class="sect2" id="language.namespaces.faq.shouldicare">
   <h3 class="title">如果我不用命名空间，是否需要关心它？</h3>
   <p class="para">
    不需要。命名空间不影响现存的代码，也不影响即将要写下的不含命名空间的代码。
    想要的话可以这样写：
   </p>
   <p class="para">
    <div class="example" id="example-317">
     <p><strong>示例 #1 在命名空间之外访问全局类</strong></p>
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$a&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;\</span><span style="color: #0000BB">stdClass</span><span style="color: #007700">;</span>
</span>
</code></div>
     </div>

    </div>
   </p>
   <p class="para">
    以上等同于：
   </p>
   <p class="para">
    <div class="example" id="example-318">
     <p><strong>示例 #2 在命名空间之外访问全局类</strong></p>
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$a&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">stdClass</span><span style="color: #007700">;</span>
</span>
</code></div>
     </div>

    </div>
   </p>
  </div>
  <div class="sect2" id="language.namespaces.faq.globalclass">
   <h3 class="title">我如何在命名空间内使用一个全局/内置的类？</h3>
   <p class="para">
    <div class="example" id="example-319">
     <p><strong>示例 #3 在命名空间内访问内置的类</strong></p>
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">namespace&nbsp;</span><span style="color: #0000BB">foo</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;\</span><span style="color: #0000BB">stdClass</span><span style="color: #007700">;<br /><br />function&nbsp;</span><span style="color: #0000BB">test</span><span style="color: #007700">(\</span><span style="color: #0000BB">ArrayObject&nbsp;$parameter_type_example&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">null</span><span style="color: #007700">)&nbsp;{}<br /><br /></span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">=&nbsp;\</span><span style="color: #0000BB">DirectoryIterator</span><span style="color: #007700">::</span><span style="color: #0000BB">CURRENT_AS_FILEINFO</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;扩展内置或全局的&nbsp;class<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">MyException&nbsp;</span><span style="color: #007700">extends&nbsp;\</span><span style="color: #0000BB">Exception&nbsp;</span><span style="color: #007700">{}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
     </div>

    </div>
   </p>
  </div>
  <div class="sect2" id="language.namespaces.faq.innamespace">
   <h3 class="title">
    如何在命名空间内访问它自己的类、函数、常量？
   </h3>
   <p class="para">
    <div class="example" id="example-320">
     <p><strong>示例 #4 在命名空间中访问内置的类、函数、常量</strong></p>
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">namespace&nbsp;</span><span style="color: #0000BB">foo</span><span style="color: #007700">;<br /><br />class&nbsp;</span><span style="color: #0000BB">MyClass&nbsp;</span><span style="color: #007700">{}<br /><br /></span><span style="color: #FF8000">//&nbsp;以当前命名空间中的&nbsp;class&nbsp;作为参数的类型<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">test</span><span style="color: #007700">(</span><span style="color: #0000BB">MyClass&nbsp;$parameter_type_example&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">null</span><span style="color: #007700">)&nbsp;{}<br /></span><span style="color: #FF8000">//&nbsp;以当前命名空间中的&nbsp;class&nbsp;作为参数的类型的另一种方式<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">test</span><span style="color: #007700">(\</span><span style="color: #0000BB">foo</span><span style="color: #007700">\</span><span style="color: #0000BB">MyClass&nbsp;$parameter_type_example&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">null</span><span style="color: #007700">)&nbsp;{}<br /><br /></span><span style="color: #FF8000">//&nbsp;在当前命名空间中扩展一个类<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">Extended&nbsp;</span><span style="color: #007700">extends&nbsp;</span><span style="color: #0000BB">MyClass&nbsp;</span><span style="color: #007700">{}<br /><br /></span><span style="color: #FF8000">//&nbsp;访问全局函数<br /></span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">=&nbsp;\</span><span style="color: #0000BB">globalfunc</span><span style="color: #007700">();<br /><br /></span><span style="color: #FF8000">//&nbsp;访问全局常量<br /></span><span style="color: #0000BB">$b&nbsp;</span><span style="color: #007700">=&nbsp;\</span><span style="color: #0000BB">INI_ALL</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
     </div>

    </div>
   </p>
  </div>
  <div class="sect2" id="language.namespaces.faq.full">
   <h3 class="title">
    像 <code class="literal">\my\name</code> 和 <code class="literal">\name</code> 这样的名称是如何解析的？
   </h3>
   <p class="para">
    以 <code class="literal">\</code> 开头的名称总是会解析成原样，
    因此 <code class="literal">\my\name</code> 实际上是 <code class="literal">my\name</code>，
    而 <code class="literal">\Exception</code> 是 <code class="literal">Exception</code>。
    <div class="example" id="example-321">
     <p><strong>示例 #5 完全限定名称</strong></p>
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">namespace&nbsp;</span><span style="color: #0000BB">foo</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;\</span><span style="color: #0000BB">my</span><span style="color: #007700">\</span><span style="color: #0000BB">name</span><span style="color: #007700">();&nbsp;</span><span style="color: #FF8000">//&nbsp;class&nbsp;"my\name"&nbsp;的实例<br /></span><span style="color: #007700">echo&nbsp;\</span><span style="color: #0000BB">strlen</span><span style="color: #007700">(</span><span style="color: #DD0000">'hi'</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;调用函数&nbsp;"strlen"<br /></span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">=&nbsp;\</span><span style="color: #0000BB">INI_ALL</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;$a&nbsp;的值设置成常量&nbsp;"INI_ALL"<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
     </div>

    </div>
   </p>
  </div>
  <div class="sect2" id="language.namespaces.faq.qualified">
   <h3 class="title">像 <code class="literal">my\name</code> 这样的名称是如何解析的？</h3>
   <p class="para">
    像 <code class="literal">my\name</code> 这样包含反斜线的名称，但不以反斜线开头的名称，
    能够以两种不同的方式解析。
   </p>
   <p class="para">
   如果有个导入语句，将其他名字设置别名为 <code class="literal">my</code>，
   则导入别名会应用到 <code class="literal">my\name</code> 的 <code class="literal">my</code> 部分。
   </p>
   <p class="para">
     如果没有导入，就会追加当前的命名空间名称为 <code class="literal">my\name</code> 的前缀。
   </p>
   <p class="para">
    <div class="example" id="example-322">
     <p><strong>示例 #6 限定名称</strong></p>
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">namespace&nbsp;</span><span style="color: #0000BB">foo</span><span style="color: #007700">;<br />use&nbsp;</span><span style="color: #0000BB">blah</span><span style="color: #007700">\</span><span style="color: #0000BB">blah&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">foo</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">my</span><span style="color: #007700">\</span><span style="color: #0000BB">name</span><span style="color: #007700">();&nbsp;</span><span style="color: #FF8000">//&nbsp;class&nbsp;"foo\my\name"&nbsp;的实例<br /></span><span style="color: #0000BB">foo</span><span style="color: #007700">\</span><span style="color: #0000BB">bar</span><span style="color: #007700">::</span><span style="color: #0000BB">name</span><span style="color: #007700">();&nbsp;</span><span style="color: #FF8000">//&nbsp;调用&nbsp;class&nbsp;"blah\blah\bar"&nbsp;的静态方法&nbsp;"name"<br /></span><span style="color: #0000BB">my</span><span style="color: #007700">\</span><span style="color: #0000BB">bar</span><span style="color: #007700">();&nbsp;</span><span style="color: #FF8000">//&nbsp;调用函数&nbsp;"foo\my\bar"<br /></span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">my</span><span style="color: #007700">\</span><span style="color: #0000BB">BAR</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;设置&nbsp;$a&nbsp;的值为&nbsp;"foo\my\BAR"<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
     </div>

    </div>
   </p>
  </div>
  <div class="sect2" id="language.namespaces.faq.shortname1">
   <h3 class="title">像 <code class="literal">name</code> 这样的非限定名称是如何解析的？</h3>
   <p class="para">
    像 <code class="literal">name</code> 这样不包含反斜线的名称，
    能够以两种不同的方式解析。
   </p>
   <p class="para">
    如果有导入语句，设置别名为 <code class="literal">name</code>，就会应用导入别名。
   </p>
   <p class="para">
    如果没有，就会把当前命名空间添加到 <code class="literal">name</code> 的前缀。
   </p>
   <p class="para">
    <div class="example" id="example-323">
     <p><strong>示例 #7 非限定类名</strong></p>
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">namespace&nbsp;</span><span style="color: #0000BB">foo</span><span style="color: #007700">;<br />use&nbsp;</span><span style="color: #0000BB">blah</span><span style="color: #007700">\</span><span style="color: #0000BB">blah&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">foo</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">name</span><span style="color: #007700">();&nbsp;</span><span style="color: #FF8000">//&nbsp;class&nbsp;"foo\name"&nbsp;的实例<br /></span><span style="color: #0000BB">foo</span><span style="color: #007700">::</span><span style="color: #0000BB">name</span><span style="color: #007700">();&nbsp;</span><span style="color: #FF8000">//&nbsp;调用&nbsp;class&nbsp;"blah\blah"&nbsp;的静态方法&nbsp;"name"<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
     </div>

    </div>
   </p>
  </div>
  <div class="sect2" id="language.namespaces.faq.shortname2">
   <h3 class="title">
    像 <code class="literal">name</code> 这样的非限定常量和函数名是如何解析的？
   </h3>
   <p class="para">
    像 <code class="literal">name</code> 这样不包含反斜线的常量和函数名，能以两种不同的方式解析。
   </p>
   <p class="para">
   首先，当前命名空间会添加到 <code class="literal">name</code> 的前缀。
   </p>
   <p class="para">
   然后，如果当前命名空间不存在函数和常量 <code class="literal">name</code>，
   而全局存在，就会使用全局的函数和常量 <code class="literal">name</code>。
   </p>
   <p class="para">
    <div class="example" id="example-324">
     <p><strong>示例 #8 非限定函数和常量名</strong></p>
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">namespace&nbsp;</span><span style="color: #0000BB">foo</span><span style="color: #007700">;<br />use&nbsp;</span><span style="color: #0000BB">blah</span><span style="color: #007700">\</span><span style="color: #0000BB">blah&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">foo</span><span style="color: #007700">;<br /><br />const&nbsp;</span><span style="color: #0000BB">FOO&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">;<br /><br />function&nbsp;</span><span style="color: #0000BB">my</span><span style="color: #007700">()&nbsp;{}<br />function&nbsp;</span><span style="color: #0000BB">foo</span><span style="color: #007700">()&nbsp;{}<br />function&nbsp;</span><span style="color: #0000BB">sort</span><span style="color: #007700">(&amp;</span><span style="color: #0000BB">$a</span><span style="color: #007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">sort</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">array_flip</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$a</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">my</span><span style="color: #007700">();&nbsp;</span><span style="color: #FF8000">//&nbsp;调用&nbsp;"foo\my"<br /></span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">strlen</span><span style="color: #007700">(</span><span style="color: #DD0000">'hi'</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;由于&nbsp;"foo\strlen"&nbsp;不存在，所以调用全局的&nbsp;"strlen"<br /></span><span style="color: #0000BB">$arr&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #0000BB">1</span><span style="color: #007700">,</span><span style="color: #0000BB">3</span><span style="color: #007700">,</span><span style="color: #0000BB">2</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$b&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">sort</span><span style="color: #007700">(</span><span style="color: #0000BB">$arr</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;调用函数&nbsp;"foo\sort"<br /></span><span style="color: #0000BB">$c&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">foo</span><span style="color: #007700">();&nbsp;</span><span style="color: #FF8000">//&nbsp;未导入，调用函数&nbsp;"foo\foo"<br /><br /></span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">FOO</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;未导入，设置&nbsp;$a&nbsp;为常量&nbsp;"foo\FOO"&nbsp;的值<br /></span><span style="color: #0000BB">$b&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">INI_ALL</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;设置&nbsp;$b&nbsp;为全局常量&nbsp;"INI_ALL"&nbsp;的值<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
     </div>

    </div>
   </p>
  </div>
  <div class="sect2" id="language.namespaces.faq.conflict">
   <h3 class="title">在同一个文件中，导入名称不能和定义的类名发生冲突</h3>
   <p class="para">
   允许以下脚本中的组合：
    <div class="informalexample">
     <p class="simpara">file1.php</p>
     <div class="example-contents">
     <div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">namespace&nbsp;</span><span style="color: #0000BB">my</span><span style="color: #007700">\</span><span style="color: #0000BB">stuff</span><span style="color: #007700">;<br />class&nbsp;</span><span style="color: #0000BB">MyClass&nbsp;</span><span style="color: #007700">{}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
     </div>

     <p class="simpara">another.php</p>
     <div class="example-contents">
     <div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">namespace&nbsp;</span><span style="color: #0000BB">another</span><span style="color: #007700">;<br />class&nbsp;</span><span style="color: #0000BB">thing&nbsp;</span><span style="color: #007700">{}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
     </div>

     <p class="simpara">file2.php</p>
     <div class="example-contents">
     <div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">namespace&nbsp;</span><span style="color: #0000BB">my</span><span style="color: #007700">\</span><span style="color: #0000BB">stuff</span><span style="color: #007700">;<br />include&nbsp;</span><span style="color: #DD0000">'file1.php'</span><span style="color: #007700">;<br />include&nbsp;</span><span style="color: #DD0000">'another.php'</span><span style="color: #007700">;<br /><br />use&nbsp;</span><span style="color: #0000BB">another</span><span style="color: #007700">\</span><span style="color: #0000BB">thing&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">MyClass</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">MyClass</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;&nbsp;class&nbsp;"thing"&nbsp;的实例来自于命名空间&nbsp;another<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
     </div>

    </div>
   </p>
   <p class="para">
    尽管在 <code class="literal">my\stuff</code> 命名空间中存在 <code class="literal">MyClass</code>，
    因为类定义在了独立的文件中，所以不会发生名称冲突。
    不过，接下来的例子中，因为 MyClass 定义在了 use 语句的同一个文件中，
    所以发生了名称冲突，导致了 fatal 错误。
    <div class="informalexample">
     <div class="example-contents">
     <div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">namespace&nbsp;</span><span style="color: #0000BB">my</span><span style="color: #007700">\</span><span style="color: #0000BB">stuff</span><span style="color: #007700">;<br />use&nbsp;</span><span style="color: #0000BB">another</span><span style="color: #007700">\</span><span style="color: #0000BB">thing&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">MyClass</span><span style="color: #007700">;<br />class&nbsp;</span><span style="color: #0000BB">MyClass&nbsp;</span><span style="color: #007700">{}&nbsp;</span><span style="color: #FF8000">//&nbsp;fatal&nbsp;error:&nbsp;MyClass&nbsp;conflicts&nbsp;with&nbsp;import&nbsp;statement<br /></span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">MyClass</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
     </div>

    </div>
   </p>
  </div>
  <div class="sect2" id="language.namespaces.faq.nested">
   <h3 class="title">不允许嵌套 namespace</h3>
   <p class="para">
    PHP 不允许嵌套 namespace
    <div class="informalexample">
     <div class="example-contents">
     <div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">namespace&nbsp;</span><span style="color: #0000BB">my</span><span style="color: #007700">\</span><span style="color: #0000BB">stuff&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;namespace&nbsp;</span><span style="color: #0000BB">nested&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;class&nbsp;</span><span style="color: #0000BB">foo&nbsp;</span><span style="color: #007700">{}<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
     </div>

    </div>
    实际上，它看上去像是这样：
    <div class="informalexample">
     <div class="example-contents">
     <div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">namespace&nbsp;</span><span style="color: #0000BB">my</span><span style="color: #007700">\</span><span style="color: #0000BB">stuff</span><span style="color: #007700">\</span><span style="color: #0000BB">nested&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;class&nbsp;</span><span style="color: #0000BB">foo&nbsp;</span><span style="color: #007700">{}<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
     </div>

    </div>
   </p>
  </div>
  <div class="sect2" id="language.namespaces.faq.quote">
   <h3 class="title">动态命名空间名称（引号标识）应该转义反斜线</h3>
   <p class="para">
     重要的是，字符串中反斜线是一个转义字符，因此在字符串中使用时，必须要写两遍。
     否则就会在无意中造成一些后果：
    <div class="example" id="example-325">
     <p><strong>示例 #9 在双引号字符串中使用命名空间的危险性</strong></p>
     <div class="example-contents">
      <div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$a&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #DD0000">"dangerous\name"</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;在双引号字符串中，\n&nbsp;是换行符！<br /></span><span style="color: #0000BB">$obj&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">$a</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #DD0000">'not\at\all\dangerous'</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;这里没有问题<br /></span><span style="color: #0000BB">$obj&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">$a</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
     </div>

    </div>
    在单引号字符串中，使用反斜线是安全的。
    但在最佳实践中，我们仍然推荐为所有字符串统一转义反斜线。
   </p>
  </div>
  <div class="sect2" id="language.namespaces.faq.constants">
   <h3 class="title">引用一个未定义的、带反斜线的常量，会导致 fatal 错误并退出</h3>
   <p class="para">
    像 <code class="literal">FOO</code> 这样的非限定名称常量，如果使用的时候还没定义，
    会产生一个 notice。PHP 会假设该常量的值是 <code class="literal">FOO</code>。
    如果没有找到包含反斜线的常量，无论是完全或者不完全限定的名称，都会产生 fatal 错误。
    <div class="example" id="example-326">
     <p><strong>示例 #10 未定义的常量</strong></p>
     <div class="example-contents">
      <div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">namespace&nbsp;</span><span style="color: #0000BB">bar</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">FOO</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;产生&nbsp;notice&nbsp;-&nbsp;undefined&nbsp;constants&nbsp;"FOO"&nbsp;assumed&nbsp;"FOO";<br /></span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">=&nbsp;\</span><span style="color: #0000BB">FOO</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;fatal&nbsp;error,&nbsp;undefined&nbsp;namespace&nbsp;constant&nbsp;FOO<br /></span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">Bar</span><span style="color: #007700">\</span><span style="color: #0000BB">FOO</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;fatal&nbsp;error,&nbsp;undefined&nbsp;namespace&nbsp;constant&nbsp;bar\Bar\FOO<br /></span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">=&nbsp;\</span><span style="color: #0000BB">Bar</span><span style="color: #007700">\</span><span style="color: #0000BB">FOO</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;fatal&nbsp;error,&nbsp;undefined&nbsp;namespace&nbsp;constant&nbsp;Bar\FOO<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
     </div>

    </div>
   </p>
  </div>
  <div class="sect2" id="language.namespaces.faq.builtinconst">
   <h3 class="title">不能重载特殊常量：NULL、TRUE、FALSE、ZEND_THREAD_SAFE、ZEND_DEBUG_BUILD</h3>
   <p class="para">
    在命名空间内定义特殊的内置常量，会导致 fatal 错误
    <div class="example" id="example-327">
     <p><strong>示例 #11 未定义的常量</strong></p>
     <div class="example-contents">
      <div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">namespace&nbsp;</span><span style="color: #0000BB">bar</span><span style="color: #007700">;<br />const&nbsp;</span><span style="color: #0000BB">NULL&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;fatal&nbsp;error;<br /></span><span style="color: #007700">const&nbsp;</span><span style="color: #0000BB">true&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'stupid'</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;也是&nbsp;fatal&nbsp;error;<br />//&nbsp;etc.<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
     </div>

    </div>
   </p>
  </div>
 </div></div></div></body></html>